package com.blue.controller.author;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.author.UserRoleDTO;
import com.blue.po.author.UserRolePO;
import com.blue.service.author.IUserRoleService;
import com.blue.service.author.IUserRoleService;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户角色关联表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/author/userRole")
@Tag(name = "用户角色关联表")
public class UserRoleController {
    @Autowired
    private IUserRoleService UserRolePOService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list(UserRoleDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        UserRolePO UserRolePO = ConvertUtils.convert(UserRolePO.class, requestDTO);
        MPJQueryWrapper<UserRolePO> queryWrapper = QueryWrapperUtils.initQueryWrapper(UserRolePO);
        Page<UserRolePO> page = new Page<>(pageNum, pageSize);
        Page<UserRolePO> pageResult = UserRolePOService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody UserRoleDTO requestDTO) {
        UserRolePO UserRolePO = ConvertUtils.convert(UserRolePO.class, requestDTO);
        UserRolePOService.save(UserRolePO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody UserRoleDTO requestDTO) {
        UserRolePO UserRolePO = ConvertUtils.convert(UserRolePO.class, requestDTO);
        UserRolePOService.updateById(UserRolePO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        UserRolePOService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody UserRoleDTO requestDTO) {
        UserRolePO UserRolePO = UserRolePOService.getById(requestDTO.getId());
        return ResponseResult.okResult(UserRolePO);
    }
}
